// 深拷贝
/**
 * @param {}        target  拷贝的对象
 * @param {WeakMap} hashMap 缓存，解决循环引用
 */
const deepClone = (target, hashMap = new WeakMap()) => {
  let result = Array.isArray(target) ? [] : {};
  if (hashMap.get(target)) return hashMap.get(target);
  if (typeof target === "object" && target !== null) {
    hashMap.set(target, result);
    if (Object.prototype.toString.call(target) === "[object Array]") {
      target.forEach((el) => {
        result.push(deepClone(el, hashMap));
      });
    } else {
      for (const key in target) {
        if (Object.hasOwnProperty.call(target, key)) {
          const value = target[key];
          result[key] = deepClone(value, hashMap);
        }
      }
    }
  } else {
    return target;
  }
  return result;
};
// 测试
const renfer = {
  arr: [12, 3, 4, 5],
};
renfer.self = renfer;
console.log(deepClone(renfer));
